// pages/due/due.js
import { imgFormat } from '../../utils/imgFormat'
import axios from '../../utils/axios'
import {toOpenDoor} from "../../utils/initTTL"
import {
  getTime
} from '../../utils/getDate'
const app = getApp();
let timeId
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    empty: false,
    enable: false,
    loadingProps: {
      size: '50rpx',
    },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // this.getList()
  },
  onRefresh() {
    this.setData({
      enable: true
    });
    this.getList()
  },
  getList(showLoading = true) {
    if(showLoading){
      wx.showLoading({
        title: '加载中',
      })
    }
    clearTimeout(timeId);
    axios.get('/app/orderList', {
      wxId: this.openid,
      status: '1,2,3',
      type:1,
    }).then(res => {
      if(res.data.length) {
        const list = res.data.map(item => {
          item.startTime = getTime(item.startTime)
          item.endTime = getTime(item.endTime)
          item.roomDetail.banners = imgFormat(item.roomDetail.banners)
          return item
        })
        this.setData({
          list,
          empty: false
        })
      } else {
        this.setData({
          list:[],
          empty: true
        })
      }
    }).finally(() => {
      timeId = setTimeout(()=>{
        this.getList(false);
      },3000)
      if(showLoading){
        wx.hideLoading();
        this.setData({
          enable: false
        })
      }
    })
  },
  toPay(e){
    const {u} = e.currentTarget.dataset;
    const orderDetail = app.globalData.orderDetail[u.ID];
    if(!orderDetail){
      wx.showToast({
        title: '订单已过期，请重新下单',
        icon: 'none'
      })
      return ;
    }
    const itemData = JSON.stringify(orderDetail);
    wx.navigateTo({
      url: `/pages/pay/pay?item=${itemData}`
    });
  },
  // 续费
  extendTime(e) {
    const {u} = e.currentTarget.dataset;
    const roomId = u.roomId;
    const orderId = u.ID;
    wx.redirectTo({
      url: '/pages/due-detail/addDuration/index?roomId='+roomId +"&orderId="+orderId,
    })
  },
  handleDevice(e){
    const {u} = e.currentTarget.dataset;
    axios.post("/app/handleDevice",{
      type:"open",
      deviceId:u,
      remote:true
    }).then(res=>{
      if(res.deviceId){
        toOpenDoor(res.deviceId)
      }else{
        if(res.msg){
          wx.showToast({
            title: res.msg,
            icon:"success"
          })
        }
      }
    })
  },
  goToDetail(e) {
    const item = e.currentTarget.dataset.item;
    wx.navigateTo({
      url: `/pages/due-detail/due-detail?ID=${item.ID}`,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.openid = wx.getStorageSync('openid');
    this.getList();
    this.getTabBar().init();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
    clearTimeout(timeId);
  },
  closeOrder(e){
    const {u} = e.currentTarget.dataset;
    axios.post("/app/closeOrder",{
      id:u.ID,
      openid:this.openid
    }).then(res=>{
      if(res.orderId){
        this.getList();
      }
    }).catch(err=>{
      setTimeout(()=>{
        this.getList();
      },1500)
    })
  },
  startOrder(e){
    const {u} = e.currentTarget.dataset;
    console.log(u)
    axios.post("/app/startOrder",{
      id:u.ID,
      openid:this.openid
    }).then(res=>{
      if(res.orderId){
        this.getList();
      }
    }).catch(err=>{
      setTimeout(()=>{
        this.getList();
      },1500)
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage(e) {
    const item = e?.target?.dataset?.u
    if(item){
      const path = '/pages/result/gift/index?id='+item.ID
      axios.post("/app/clearGiftId",{
        id:item.ID
      })
      return {
        title: '转赠订单',
        path,
        success: function (res) {
          // 分享成功后的操作
        },
        fail: function (res) {
          // 分享失败后的操作
        }
      };
    }else{
      return {

      }
    }
    
  }
})